package com.fry.mall.wallet.response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * 统一返回类型
 *
 * @Author: FRY
 * @CreateTime: 2023-03-02  21:39
 */
public class ResponseResult<T> implements Serializable {

    private static final Logger log = LoggerFactory.getLogger(ResponseResult.class);
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 信息
     */
    private String msg;

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 数据
     */
    private T data;


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Boolean getSuccess() {
        return success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResponseResult{" + "code=" + code + ", msg='" + msg + "', success=" + success + '\'' + ", data=" + data + '}';
    }

    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(ResponseEnum.OK.getCode());
        responseResult.setSuccess(true);
        responseResult.setData(data);
        return responseResult;
    }

    public static <T> ResponseResult<T> success() {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setMsg(ResponseEnum.OK.getMsg());
        responseResult.setSuccess(true);
        responseResult.setCode(ResponseEnum.OK.getCode());
        return responseResult;
    }

    /**
     * 前端显示失败消息
     *
     * @param msg 失败消息
     * @return
     */
    public static <T> ResponseResult<T> showFailMsg(String msg) {
        log.error(msg);
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(ResponseEnum.SYSTEM_VARIOUS_FAIL.getCode());
        responseResult.setMsg(msg);
        responseResult.setSuccess(false);
        return responseResult;
    }

    public static <T> ResponseResult<T> fail(ResponseEnum responseEnum) {
        log.error(responseEnum.toString());
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(responseEnum.getCode());
        responseResult.setMsg(responseEnum.getMsg());
        responseResult.setSuccess(false);
        return responseResult;
    }

    public static <T> ResponseResult<T> fail(ResponseEnum responseEnum, T data) {
        log.error(responseEnum.toString());
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(responseEnum.getCode());
        responseResult.setMsg(responseEnum.getMsg());
        responseResult.setSuccess(false);
        responseResult.setData(data);
        return responseResult;
    }

    public static <T> ResponseResult<T> transform(ResponseResult<?> oldResponseResult) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setMsg(oldResponseResult.getMsg());
        responseResult.setCode(oldResponseResult.getCode());
        responseResult.setSuccess(oldResponseResult.getSuccess());
        log.error(responseResult.toString());
        return responseResult;
    }

}


